package com.ifast.hnzk.controller;


import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ifast.common.domain.DictDO;
import com.ifast.common.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.hnzk.domain.MessageDO;
import com.ifast.hnzk.service.MessageService;
import com.ifast.common.utils.Result;

/**
 *
 * <pre>
 *
 * </pre>
 * <small> 2019-07-04 15:35:41 | wch</small>
 */
@Controller
@RequestMapping("/hnzk/message")
public class MessageController extends BaseController {
	@Autowired
	private MessageService messageService;
	@Autowired
	private DictService dictService;

	@GetMapping()
	@RequiresPermissions("hnzk:message:message")
	String Message(){
		return "hnzk/message/message";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("hnzk:message:message")
	public Result<Page<MessageDO>> list(MessageDO messageDTO){
		Wrapper<MessageDO> wrapper = new EntityWrapper<MessageDO>();
		wrapper.like("name",messageDTO.getName());
		wrapper.eq(messageDTO.getStatus()!=null,"status",messageDTO.getStatus());
		wrapper.eq(messageDTO.getBuildingHead()!=null && messageDTO.getBuildingHead()!= "","buildingHead",messageDTO.getBuildingHead());
		wrapper.orderBy("id", false);
		Page<MessageDO> page = messageService.selectPage(getPage(MessageDO.class), wrapper);
		return Result.ok(page);
	}

	@GetMapping("/add")
	@RequiresPermissions("hnzk:message:add")
	String add(Model model){
		/**
		 * 查询性别列表
		 */
		Wrapper<DictDO> wrapper = new EntityWrapper<DictDO>();
		wrapper.eq("type","sex");
		List<DictDO> dictList = dictService.selectList(wrapper);
		/**
		 * 查询房屋性质列表
		 */
		Wrapper<DictDO> wrapperNature = new EntityWrapper<DictDO>();
		wrapperNature.eq("type","house_nature");
		List<DictDO> houseNatureList = dictService.selectList(wrapperNature);
		model.addAttribute("dictList",dictList);
		model.addAttribute("houseNatureList",houseNatureList);
		return "hnzk/message/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("hnzk:message:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MessageDO message = messageService.selectById(id);

		/**
		 * 查询房屋性质列表
		 */
		Wrapper<DictDO> wrapperNature = new EntityWrapper<DictDO>();
		wrapperNature.eq("type","house_nature");
		List<DictDO> houseNatureList = dictService.selectList(wrapperNature);
		model.addAttribute("message", message);
		model.addAttribute("houseNatureList",houseNatureList);
		return "hnzk/message/edit";
	}

	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("hnzk:message:add")
	public Result<String> save( MessageDO message){
		message.setUpdateDate(new Date());
		message.setCreateDate(new Date());
		messageService.insert(message);
		return Result.ok();
	}

	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("hnzk:message:edit")
	public Result<String>  update( MessageDO message){
		message.setUpdateDate(new Date());
		boolean update = messageService.updateById(message);
		return update ? Result.ok() : Result.fail();
	}

	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("hnzk:message:remove")
	public Result<String>  remove( Long id){
		messageService.deleteById(id);
		return Result.ok();
	}

	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("hnzk:message:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		messageService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

}
